package com.ddshuai.middle;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述 钥匙和房间
 *
 * @author ddshuai
 * @date 2020-08-31 17:18
 **/
public class CanVisitAllRooms {

    public static void main(String[] args) {

        List<List<Integer>> datas = new ArrayList<>();
        datas.add(Arrays.asList(1,3));
        datas.add(Arrays.asList(3,0,1));
        datas.add(Arrays.asList(2));
        datas.add(Arrays.asList(0));
        System.out.println(canVisitAllRooms(datas));
    }

    public static boolean canVisitAllRooms(List<List<Integer>> rooms) {

        for (int i = 0; i < rooms.size(); i++) {
            //当前房间内所有的钥匙
            List<Integer> keys = rooms.get(i);
            int nextRoom = i + 1;
            if (nextRoom == rooms.size()) {
                return true;
            }
            boolean hasNextRoomKey = false;
            for (Integer key : keys) {
                if (key == nextRoom) {
                    hasNextRoomKey = true;
                    break;
                }
            }

            if (!hasNextRoomKey) {
                return false;
            }
        }

        return true;
    }
}
